/***************************************************************************
 *
 * File name: q2.c
 *
 * Author:  Tomer Filiba
 *          03701307-5
 *          sebulba@t2
 *
 *
 * Exercise #: 2
 * Question #: 2
 *
 * Submission Date: 11/6
 *
 **************************************************************************/

/*-------------------------------------------------------------------------
  Include files:
--------------------------------------------------------------------------*/
#include <stdio.h>
#include <math.h>


/*=========================================================================
  Constants and definitions:
==========================================================================*/
#define GREAT_SUCCESS		(0)
#define I_WILL_BE_EXECUTE	(1)
#define PRECISION			(0.001)


typedef struct
{
	double x;
	double y;
} coord_t;


/*-------------------------------------------------------------------------
  calc the distance between two points (pythagorean equation)

  Parameters:
  p1 - first point (pointer)
  p2 - second point (pointer)

  Return value: the distance
 -------------------------------------------------------------------------*/
double distance(const coord_t * p1, const coord_t * p2)
{
	double dx, dy;
	dx = p1->x - p2->x;
	dy = p1->y - p2->y;
	return sqrt(dx*dx + dy*dy);
}


/*-------------------------------------------------------------------------
  The main program: read three coordinates from the user, and print the
  edge length, perimeter and area of the triangle defined by these
  points
 -------------------------------------------------------------------------*/
int main(int argc, const char ** argv)
{
	coord_t p1, p2, p3;
	double a, b, c, q, diff, perim, area;
	
	/* get input from user (three points) */
	printf("Enter p1: ");
	if (scanf(" ( %lf , %lf )", &p1.x, &p1.y) != 2) {
		printf("Error! Invalid format\n");
		return I_WILL_BE_EXECUTE;
	}
	printf("Enter p2: ");
	if (scanf(" ( %lf , %lf )", &p2.x, &p2.y) != 2) {
		printf("\nError! Invalid format\n");
		return I_WILL_BE_EXECUTE;
	}
	printf("Enter p3: ");
	if (scanf(" ( %lf , %lf )", &p3.x, &p3.y) != 2) {
		printf("\nError! Invalid format\n");
		return I_WILL_BE_EXECUTE;
	}
	
	/* make sure the points are not collinear */
	diff = (p3.y - p1.y) * (p2.x - p1.x) - (p3.x - p1.x) * (p2.x - p1.x);
	if (fabs(diff) < PRECISION) {
		printf("Error! The three points are collinear\n");
		return I_WILL_BE_EXECUTE;
	}
	
	/* calc edges, perimeter, and area */
	a = distance(&p1, &p2);
	b = distance(&p2, &p3);
	c = distance(&p3, &p1);
	perim = a + b + c;
	q = perim / 2;
	area = sqrt(q * (q - a) * (q - b) * (q - c));
	
	printf("Edges length: %.2f, %.2f, %.2f\n", a, b, c);
	printf("Perimeter: %.2f\n", perim);
	printf("Area: %.2f\n", area);
	
	return GREAT_SUCCESS;
}



/**************************************************************************
			                          End of file
***************************************************************************/